﻿@model List<BDShop.Models.OrderDetailModels>
@using BDShop.Business.Cache;
@using BDShop.Entities;
@using BDShop.Configuration;
@using BDShop.Framework.Extensions;
@using BDShop.Utilities.Managers;
@using BDShop.Web;

<section id="cart">
    <div class="heading">
        <h4>
            <img width="32" height="32" alt="" src="/Images/cart-bg.png"></h4>
        <a><span id="cart-total">(@Model.Count)</span></a>
    </div>
    @if (Model.Count > 0)
    {
        <div class="content">
            <div class="mini-cart-info">
                <table>
                    @{int sum = 0;}
                    @foreach (var order in Model)
                    {
                        ProductCacheComplex product = ProductCache.Instance.Get().Where(p => p.ID == order.productID).FirstOrDefault();
                        Constant property = ConstantCache.Instance.Get().Where(c => c.Value == order.propertyID).FirstOrDefault();
                        int total = order.price * order.quantity;
                        sum += total;

                        string[] categoryDefinition = null;
                        if (!string.IsNullOrEmpty(product.CategoryDefinition))
                        {
                            categoryDefinition = product.CategoryDefinition.Split('|')[0].Split('#');
                        }
                        var url = UrlManager.ProductDetailUrl(product.ID, product.Name, categoryDefinition);
                        <tr>
                            <td class="image"><a href="@url">
                                <img style="width:45px" class="lazy" data-original="@Path.Combine(BDShopConfiguration.Storage.MainImagePath, product.MainImage)" src="@Path.Combine(BDShopConfiguration.Storage.MainImagePath, product.MainImage)" alt="@product.Name" title="@product.Name" /></a></td>
                            <td class="name"><a href="@url">@product.Name</a></td>
                             <td class="model">@property.Description</td>
                            <td class="quantity">@order.quantity</td>
                            <td class="total">@total.ToCurrencyString(false, true)</td>
                            <td class="remove">
                                <img id="imgDelete" src="/Images/remove-small.png" alt="Xóa" title="Xóa" onclick="RemoveProductOfCart(@order.productID,'@order.propertyID');" />
                            </td>
                        </tr>
                    }
                </table>
            </div>
            <div class="mini-cart-total">
                <table>
                    <tr>
                        <td class="right"><b>Tổng cộng:</b></td>
                        <td class="right">@sum.ToCurrencyString(false, true)</td>
                    </tr>
                    <tr>
                        <td class="right"><b>Thành tiền:</b></td>
                        <td class="right">@sum.ToCurrencyString(false, true)</td>
                    </tr>
                </table>
            </div>
            <div class="checkout"><a class="button" href="/gio-hang.html">Giỏ hàng</a> &nbsp; <a class="button" href="/thanh-toan.html">Thanh toán</a></div>
        </div>
    }
</section>
<script>
    function RemoveProductOfCart(pID,proID) {
        $.post("/Cart/RemoveProductOfCart", {productID:pID,ddlColor:proID})
               .done(function (json) {
                   json = json || {};
                   if (json.success) {
                       location.reload();
                   } else if (json.errors) {
                       alert("Có lỗi trong quá trình hủy sản phẩm trong giỏ hàng.");
                   }
               })
               .error(function () {
                   alert("Có lỗi trong quá trình hủy sản phẩm trong giỏ hàng.");
               });
    };
</script>